﻿/// <reference path="../Shell/base.js" />
/// <reference path="../../jquery-1.2.6-vsdoc.js" />

(function($) {

    $.widget("ui.epiFeedback", {
        /// <summary>
        ///     epiFeedback widget.
        ///     Depends: ui.core.js
        /// </summary>
        _init: function() {
            /// <summary>
            ///     epiFeedback widget initialization.
            /// </summary>

            var options = this.options;
            var self = this;

            (this.feedback = this.element).addClass(options.feedbackClass)
                .attr("role", "alert");
            (this.feedbackArea = $("<span/>")).addClass(options.feedbackContentClass).appendTo(this.feedback);

            $(document).bind("epiajaxrequestrunning", null, function(e, data) { self._handleAjaxClearFeedback(e, data); });

        },
        _handleAjaxClearFeedback: function(e, data) {
            /// <summary>
            ///     Eventhandler to take care of the epiajaxrequestrunning event. 
            ///     The feedback should be cleared if keepFeedback in the eventdata is false.
            /// </summary>
            /// <param name="e" type="Event">
            ///     The event object sent with the event.
            /// </param>
            /// <param name="data" type="Object">
            ///     The data sent from the server containing all definitions.
            /// </param>
            if (typeof data === "undefined" || typeof data.eventData === "undefined" || data.eventData.keepFeedback !== true) {
                this.hide();
            }
        },
        setErrorMessage: function(message, responseText) {
            var sharedResourceKey = "EPiServer.Shell.Resources.Texts";
            this.setMessage(
                message,
                { showExtendedMessageButton: {
                    text: epi.shell.resource.get(sharedResourceKey, "ErrorMoreInformationText"),
                    description: epi.shell.resource.get(sharedResourceKey, "ErrorMoreInformationTitle"),
                    click: function(e) {
                        epi.shell.error.openErrorDialog(this, responseText);
                    }
                }
                }, true);
        },

        setMessage: function(message, actions, isError) {
            /// <summary>
            ///     Sets a new message in the feedback area of the dashboard.
            /// </summary>
            /// <param name="message" type="String">
            ///     The textmessage to display.
            /// </param>
            /// <param name="buttons" type="Object">
            ///     Buttons to place in the feedback toolbar. 
            ///     Here is a example of a button object:
            //  SetMessage( "Some text", {exampleButton: {
            //      text: "text",               // The text inside the link (button).
            //      description: "no-title",          // The title of the link (button).
            //      buttonClass: "no-class",    // The class of the link (button icon).
            //      click: function(e) { },     // The function to run link is clicked
            //      tabIndex: -1                // What tabindex the button should have
            //  }})
            /// </param>
            var self = this;
            var o = this.options;
            var item;

            if (typeof actions == "undefined") {
                actions = {};
            }

            this.feedbackArea.html($("<span/>").addClass(o.feedbackLabelClass).append(message));

            $.each(actions, function() {
                var action = this;
                item = $('<a/>').attr("href", "#")
                    .attr("title", action.description)
                    .attr("class", o.feedbackActionClass)
                    .append('<span>' + action.text + '</span>')
                    .appendTo(self.feedbackArea);

                if (isError) {
                    self.feedbackArea.addClass("epi-feedbackContent-error");
                    self.feedbackArea.removeClass("epi-feedbackContent-info");
                }
                else {
                    self.feedbackArea.removeClass("epi-feedbackContent-error");
                    self.feedbackArea.addClass("epi-feedbackContent-info");
                }

                if (typeof action.click == "function") {
                    item.click(action.click);
                }
            });

            $('<a/>')
                .attr("href", "#")
                .addClass(o.feedbackCloseButtonClass)
                .attr("title", o.closeButtonDescription)
                .bind("click", function(e) { self.hide(); })
                .append('<span>' + o.closeButtonText + '</span>')
                .appendTo(this.feedbackArea);

            this.show();
        },
        hide: function() {
            /// <summary>
            ///     Hide the feedback area.
            /// </summary>
            this.feedback.hide();
            this.feedback.attr("aria-hidden", "true");
        },
        show: function() {
            /// <summary>
            ///     Show the feedback area.
            /// </summary>
            this.feedback.show();
            this.feedback.attr("aria-hidden", "false");
        },
        transferToFeedback: function(from, callback) {
            /// <summary>
            ///     Displays a transfereffect to the feedback area.
            /// </summary>
            /// <param name="from" type="jQuery">
            ///     The object that will be used to start the transfer effect.
            /// </param>
            /// <param name="callback" type="Function">
            ///     A function to call after the transfer effect is complete.
            /// </param>
            from.hide("transfer", { to: "#feedback", className: this.options.feedbackTransferClass }, 250, callback);
        },
        transferFromFeedback: function(to, callback) {
            /// <summary>
            ///     Displays a transfereffect from the feedback area.
            /// </summary>
            /// <param name="to" type="String">
            ///     A jQuery selector string for the object to transfer too.
            /// </param>
            /// <param name="callback" type="Function">
            ///     A function to call after the transfer effect is complete.
            /// </param>
            this.element.hide("transfer", { to: to, className: this.options.feedbackTransferClass }, 250, callback);
        }
    });

    $.extend($.ui.epiFeedback, {
        version: "0.1",
        eventPrefix: "epifeedback",
        defaults: {
            feedbackClass: "epi-feedback",
            feedbackContentClass: "epi-feedbackContent",
            feedbackLabelClass: "epi-feedbackLabel",
            feedbackTransferClass: "epi-effects-transfer",
            feedbackActionClass: "epi-feedback-action",
            feedbackCloseButtonClass: "epi-feedback-closeButton epi-iconToolbar-delete2",
            closeButtonText: "Close",
            closeButtonDescription: "Close message"
        }
    });

})(epiJQuery);